package com.itheima.config;

import cn.hutool.crypto.SecureUtil;
import com.itheima.auth.filter.JwtAuthenticationTokenFilter;
import com.itheima.auth.handle.MyAccessDeniedHandler;
import com.itheima.auth.handle.MyAuthenticationEntryPoint;
import com.itheima.auth.handle.MyAuthenticationFailureHandler;
import com.itheima.auth.handle.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
    @Resource
    private MyAccessDeniedHandler myAccessDeniedHandler;
    @Resource
    private MyAuthenticationEntryPoint myAuthenticationEntryPoint;
    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    /**
     * 自定义加密器
     * */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return SecureUtil.md5(String.valueOf(rawPassword));
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                // md5加密
                return this.encode(rawPassword).equals(encodedPassword);
            }
        };
    }

    /**
     * 静态资源放行
     * */
    @Override
    public void configure(WebSecurity web) throws Exception {
        //swagger-ui放行
        web.ignoring().antMatchers("/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html"
        );
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin() //认证相关配置
                .successHandler(myAuthenticationSuccessHandler) //成功处理
                .failureHandler(myAuthenticationFailureHandler) //失败处理
            .and()
            .exceptionHandling()
                .accessDeniedHandler(myAccessDeniedHandler)//403无权限访问处理
                .authenticationEntryPoint(myAuthenticationEntryPoint)
            .and()
            .authorizeRequests()//拦截所有的请求配置(除了/login.html,/login)
                .anyRequest().authenticated()//authenticated需要认证通过(登入成功)
            .and()
            .csrf().disable() //禁用csrf
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) //禁用session
            .and()
            //添加执行链过滤器
            .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

}
